//
//  CZHomeListViewModel.swift
//  CZVVeiBo
//
//  Created by smile on 2017/8/2.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit
import SDWebImage
class CZHomeListViewModel: NSObject {
    // 保存首页数据的模型数组
    // 保存模型的数组
    lazy var dataList:[CZHomeViewModel] = [CZHomeViewModel]()
}
/*
 - 帮助首页请求数据
 - 保存数据
 - 提供数据
 */
extension CZHomeListViewModel{
    func getHomeData(isPullUp: Bool, isFinish:@escaping (Bool, Int)->()){
        
        // 定义sinceid he maxid
        var since_id: Int64 = 0
        var max_id: Int64 = 0
        
        // 如果是上拉加载更多
        if isPullUp {
            max_id = dataList.last?.homeModel?.id ?? 0
            // 则返回ID小于或等于max_id的微博
            if max_id > 0 {
                max_id -= 1
            }
        }else{
            // 下拉刷新
            since_id = dataList.first?.homeModel?.id ?? 0
        }
        
        CZNetworkTools.share.getHome(since_id: since_id, max_id: max_id, success: { (response) in
            
            // 判断response是否为nil 而且是否可以转成字典
            guard let resDict = response as? [String: Any] else{
                isFinish(false, 0)
                return
            }
            
            // 判断resArr 是否为nil 而且是否可以转成一个字典数组
            guard let resArr = resDict["statuses"] as? [[String: Any]] else{
                isFinish(false, 0)
                return
            }
            // 字典数组转模型数组
            guard let statusList = NSArray.yy_modelArray(with: CZHomeModel.self, json: resArr) as? [CZHomeModel] else{
                isFinish(false, 0)
                return
            }
            
            // 创建一个临时可变的空数组->[CZHomeViewModel]
            
            var tempList:[CZHomeViewModel] = [CZHomeViewModel]()
            
            let group = DispatchGroup()
            
            // 遍历statusList
            for model in statusList{
                // 创建viewmodel
                let homeViewModel = CZHomeViewModel()
                // 赋值
                homeViewModel.homeModel = model
                // 添加到数组中
                tempList.append(homeViewModel)
                
                
                if model.pic_urls?.count == 1 {
                    group.enter()
                    SDWebImageManager.shared().loadImage(with: URL(string: model.pic_urls?.first?.thumbnail_pic ?? ""), options: [], progress: nil, completed: { (image, _, _, _, _, _) in
                        print("原创完成")
                        group.leave()
                    })
                    
                }
                
                if model.retweeted_status?.pic_urls?.count == 1{
                    
                    group.enter()
                    SDWebImageManager.shared().loadImage(with: URL(string: model.retweeted_status?.pic_urls?.first?.thumbnail_pic ?? ""), options: [], progress: nil, completed: { (image, _, _, _, _, _) in
                        print("转发完成")
                        group.leave()
                    })
                }
            }
            
            // 赋值
            // 如果是上拉加载更多
            if isPullUp{
                self.dataList = self.dataList + tempList
            }else{
                // 判断如果是下拉刷新
                self.dataList = tempList + self.dataList
            }
            group.notify(queue: DispatchQueue.main, execute: {
                // 成功
                isFinish(true, tempList.count)
                print("最后完成")
            })
        }) { (error) in
            print("失败", error)
            isFinish(false, 0)
        }
        
    }
}
